package com.meta.menu.service.impl;

import com.meta.common.CommonInfo;
import com.meta.menu.bo.RouteMenu;
import com.meta.menu.service.UserMenuService;
import com.meta.nd.service.NvService;
import com.meta.nd.vo.Nv;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;


@Component
public class UserMenuServiceImpl implements UserMenuService {
    @Resource
    NvService nvService;


    @Override
    public RouteMenu getRouteMenu(String userCode) {
        Nv root = nvService.getNvByCode(CommonInfo.getDefault(),"1","code","routeMenuNd");
        if ( root == null)
            throw new RuntimeException("路由菜单没有代码为1的根菜单!");
        RouteMenu routeMenu = nv2RouteMenu(root);
        List<String> menuCodes = this.getMenuCodesByUserCode(userCode);
        clipMenuByUserCode(routeMenu,menuCodes);
        return routeMenu;
    }

    private List<String> getMenuCodesByUserCode(String userCode){
        Nv userNv = nvService.getNvByCode(CommonInfo.getDefault(),userCode,"userCode","userNd");
        if ( userNv == null )
            throw new RuntimeException("不存在的用户代码:" + userCode);
        return userNv.getStringListByName("menuCodes");

    }

    private void clipMenuByUserCode(RouteMenu routeMenu, List<String> menuCodes){
        String code = routeMenu.getCode();
        if (routeMenu.isLeaf() && !menuCodes.contains(code))
            return;

        for( int i= routeMenu.getChildren().size() -1; i>=0; i--){
            RouteMenu child = routeMenu.getChildren().get(i);
            code = child.getCode();
            if ( child.isLeaf() && !menuCodes.contains(code)) {
                routeMenu.getChildren().remove(i);
            }
            else if ( !child.isLeaf() ){
                clipMenuByUserCode(child,menuCodes);
                if ( child.getChildren().size() == 0 )
                    routeMenu.getChildren().remove(child);
            }
        }
    }

    public RouteMenu nv2RouteMenu(Nv nv){
        if ( nv == null)
            return null;
        RouteMenu routeMenu = new RouteMenu();
        routeMenu.setCode(nv.getRawStringByName("code"));
        routeMenu.setPath(nv.getRawStringByName("path"));
        routeMenu.setComponent(nv.getRawStringByName("component"));
        routeMenu.setRedirect(nv.getRawStringByName("redirect"));
        routeMenu.setLeaf(nv.getRawBoolByName("leaf"));
        routeMenu.getMeta().setTitle(nv.getRawStringByName("title"));
        routeMenu.getMeta().setHideTabs(nv.getRawBoolByName("hideTabs"));
        String moduleCode = nv.getRawStringByName("moduleCode");
        if ( !StringUtils.isEmpty(moduleCode)){
            routeMenu.getMeta().setModuleCode(moduleCode);
            routeMenu.getMeta().setMdCode(nv.getRawStringByName("mdCode"));
            routeMenu.getMeta().setMvCode(nv.getRawStringByName("mvCode"));
        }
        for( Nv childNv : nv.getChildNvs("routeMenuNd")){
            RouteMenu child = this.nv2RouteMenu(childNv);
            if (child!=null)
                routeMenu.getChildren().add(child);
        }
        return routeMenu;
    }
}
